package com.atguigu.tingshu.user.context;

import com.atguigu.tingshu.user.strategy.UpdateUserPaidRecordStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author  王东剑
 * Created on 24/6/2024 下午8:02
 * Package_name com.atguigu.tingshu.user.context
 * Description
 */
@Component
public class UpdateUserPaidRecordStrategyContext implements ApplicationContextAware {
	private static final Map<String, UpdateUserPaidRecordStrategy> UPDATE_USER_PAID_RECORD_STRATEGY_MAP = new ConcurrentHashMap<>();
	public static UpdateUserPaidRecordStrategy getUpdateUserPaidRecordStrategy(String itemType){
		return UPDATE_USER_PAID_RECORD_STRATEGY_MAP.get(itemType);
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, UpdateUserPaidRecordStrategy> beansOfType = applicationContext.getBeansOfType(UpdateUserPaidRecordStrategy.class);
		beansOfType.values().forEach(updateUserPaidRecordStrategy -> {
			UPDATE_USER_PAID_RECORD_STRATEGY_MAP.put(updateUserPaidRecordStrategy.getItemType(),updateUserPaidRecordStrategy);
		});
	}
}
